﻿using M.Warehouse.API.Application.Command.ReservoirsCommand;
using M.Warehouse.API.Application.Command.WarehousesCommand;
using M.Warehouse.Domain;
using M.Warehouse.Domain.DTO;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M.Warehouse.API.Controllers
{
    /// <summary>
    /// 库区控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ReservoirsController : ControllerBase
    {
        private readonly IMediator _mediator;
        public ReservoirsController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加库区
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(ReservoirsCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改库区
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Update(ReservoirsUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除库区
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Delete([FromQuery]ReservoirsDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量删除库区
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDel(ReservoirsBatchDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示库区
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<ReservoirsShowDTO>>> Show([FromQuery]ReservoirsShowCommand command)
        {
            ApiResult<PageDTO<ReservoirsShowDTO>> result = new ApiResult<PageDTO<ReservoirsShowDTO>>();
            try
            {
                result.Data=await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 返填库区
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Reservoirs>> Backfill([FromQuery] ReservoirsBackfillCommand command)
        {
            ApiResult<Reservoirs> result = new ApiResult<Reservoirs>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 通过仓库id显示库区
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Reservoirs>>> ShowReservoirsByWarehouseId([FromQuery] ReservoirsShowByWarehouseIdCommand command)
        {
            ApiResult<List<Reservoirs>> result = new ApiResult<List<Reservoirs>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
